package com.litt.saap.core.rest;

import java.util.List;

import org.jsondoc.core.pojo.JSONDoc;
import org.jsondoc.core.pojo.JSONDoc.MethodDisplay;
import org.jsondoc.core.scanner.JSONDocScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class JSONDocController {
  private String version;
  
  @Value("#{propConfig['rest.basePath']}")
  private String basePath;
  private List<String> packages;
  private JSONDocScanner jsondocScanner;
  private boolean playgroundEnabled = true;
  private MethodDisplay displayMethodAs = MethodDisplay.URI;

  public final static String JSONDOC_DEFAULT_PATH = "/jsondoc";

  public JSONDocController(String version, List<String> packages) {
    this.version = version;    
    this.packages = packages;
    this.jsondocScanner = new SpringJSONDocScanner();
  }

  public boolean isPlaygroundEnabled() {
    return playgroundEnabled;
  }

  public void setPlaygroundEnabled(boolean playgroundEnabled) {
    this.playgroundEnabled = playgroundEnabled;
  }

  public MethodDisplay getDisplayMethodAs() {
    return displayMethodAs;
  }

  public void setDisplayMethodAs(MethodDisplay displayMethodAs) {
    this.displayMethodAs = displayMethodAs;
  }

  @RequestMapping(value = JSONDocController.JSONDOC_DEFAULT_PATH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody JSONDoc getApi() {
    return jsondocScanner.getJSONDoc(version, basePath, packages, playgroundEnabled, displayMethodAs);
  }

}
